package star.core.util.graph;

public class GTNode<T> {
	private T data = null;
	private GTNode<T> parent = null;
	private GTNode<T> child = null;
	private GTNode<T> sibling = null;
	private int size = 0;

	public GTNode(T data) {
		this.data = data;
	}
	
	public T value() {return data;}
	public GTNode<T> parent() {return parent;}
	public GTNode<T> child() {return child;}
	public GTNode<T> sibling() {return sibling;}
	
	public boolean isLeaf() {
		if(child == null)
			return true;
		return false;
	}
	public void setData(T data) {
		this.data = data;
	}
	public GTNode<T> addChild(T data) {
		if(data == null)
			return null;
		size++;
		if(child == null) {
			child = new GTNode<T>(data);
			child.parent = this;
			return child;
		}
		GTNode<T> node = child;
		while(node.sibling != null) {
			node = node.sibling;
		}
		node.sibling = new GTNode<T>(data);
		node.sibling.parent = this;
		return node.sibling;
	}
	public void addSubTree(GTNode<T> rootNode) {
		size++;
		if(child == null) {
			child = rootNode;
			child.parent = this;
			return;
		}
		GTNode<T> node = child;
		while(node.sibling != null) {
			node = node.sibling;
		}
		node.sibling = rootNode;
		node.sibling.parent = this;
	}

	public int size() {
		return size;
	}
	public void removeChild() {child = null;} 
	public void removeSibling() {sibling = null;}

	@Override
	public String toString() {
		return "GTNode [data=" + data + "]";
	} 
}
